﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Common.Silverlight;
using SMS.Controls.AuthenticationSVC;
using System.ComponentModel;

namespace SMS.Controls
{
    public partial class SMSUsers : UserControl
    {
        private ServiceProxy<AuthenticationServiceClient> proxy = null;
       public SMSUsers()
        {
            InitializeComponent();
            if (!DesignerProperties.IsInDesignTool)
            {
                proxy = new ServiceProxy<AuthenticationServiceClient>("Service/AuthenticationService.svc");
                proxy.Client.GetApplicationUserListCompleted += new EventHandler<GetApplicationUserListCompletedEventArgs>(Client_GetApplicationUserListCompleted);
                smsAutoComplete1.SelectionChanged += new Action<object, object>(smsAutoComplete1_SelectionChanged);
                smsAutoComplete1.SearchTextChanged += new Action<string>(smsAutoComplete1_SearchTextChanged);
                smsAutoComplete1.TextChanged += new Action<string>(smsAutoComplete1_TextChanged);
                smsAutoComplete1.SetFilter(SearchUsers);
                smsAutoComplete1.SetDataTemplate(this.Resources["UsersTemplate"] as DataTemplate);
                smsAutoComplete1.SetValuePath("UserName");
            }
        }

       void Client_GetApplicationUserListCompleted(object sender, GetApplicationUserListCompletedEventArgs e)
       {
           smsAutoComplete1.ItemSource = e.Result as IEnumerable<SMS.Entities.BaseClasses.BaseEntity>;
           VisualStateManager.GoToState(smsAutoComplete1, "Normal", true);   
       }

        void smsAutoComplete1_TextChanged(string obj)
        {
            SearchText = obj;
        }

       
        private bool IsNewSearchTextRequired(string searchText)
        {
            return currentSearchText.ToLower() != searchText.ToLower();
        }

        private string currentSearchText = string.Empty;

        public bool SearchUsers(string search, object value)
        {
            return (value as SMS.Entities.Admin.ApplicationUser).Login_ID.ToString().Contains(search.ToLower()) ||
                (value as SMS.Entities.Admin.ApplicationUser).UserName.ToLower().Contains(search.ToLower()) ;
            //return true;
        }

        void smsAutoComplete1_SearchTextChanged(string obj)
        {

            if (IsNewSearchTextRequired(obj))
            {
                currentSearchText = smsAutoComplete1.autoCompleteBox1.Text;
                VisualStateManager.GoToState(smsAutoComplete1, "BusyState", true);
                proxy.Client.GetApplicationUserListAsync(string.Empty, obj);
            }
            SearchText = obj;
        }

        void smsAutoComplete1_SelectionChanged(object arg1, object arg2)
        {
            SelectedUser = arg2 as Entities.Admin.ApplicationUser;
        }

        public Entities.Admin.ApplicationUser SelectedUser
        {
            get { return (Entities.Admin.ApplicationUser)GetValue(SelectedUserProperty); }
            set
            {
                SetValue(SelectedUserProperty, value);
                smsAutoComplete1.SetSelectedItem(value);
            }
        }

        // Using a DependencyProperty as the backing store for SelectedProduct.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedUserProperty =
            DependencyProperty.Register("SelectedUser", typeof(Entities.Admin.ApplicationUser), typeof(SMSUsers), new PropertyMetadata(null, new PropertyChangedCallback(SelectedUserChanged)));

        public static void SelectedUserChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as SMSUsers;
            //if (control.SelectedProduct != e.NewValue as SMS.Entities.Product)
            //{
            control.SelectedUser = e.NewValue as Entities.Admin.ApplicationUser;
            //}
        }

        public string SearchText
        {
            get { return (string)GetValue(SearchTextProperty); }
            set { SetValue(SearchTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SearchText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SearchTextProperty =
            DependencyProperty.Register("SearchText", typeof(string), typeof(SMSUsers), new PropertyMetadata(string.Empty, new PropertyChangedCallback(SearchTextPropertyChanged)));

        private static void SearchTextPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as SMSUsers;
            control.SearchText = e.NewValue as string;
        }

    }
}
